/*
 * Copyright 2010 Shumpei Shiraishi
 */
package net.fivegoods.entity.impl.slim3;


@javax.ws.rs.Path("mission_executions")
@org.slim3.datastore.Model
@javax.persistence.Entity
@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.NONE)
@javax.xml.bind.annotation.XmlRootElement
@javax.annotation.Generated("momo.annotation.processing.MomoProcessor")
public class MissionExecutionModel implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	
	@com.google.inject.Inject
	static javax.validation.Validator validator;
	
	@javax.ws.rs.Path("/{key}")
	@javax.ws.rs.GET
	public static net.fivegoods.entity.impl.slim3.MissionExecutionModel getByKeyString(
		@javax.ws.rs.PathParam("key") java.lang.String keyStr) {
		return getByKey(com.google.appengine.api.datastore.KeyFactory.stringToKey(keyStr));
	}
	
	public static net.fivegoods.entity.impl.slim3.MissionExecutionModel getByKey(com.google.appengine.api.datastore.Key key) {
		return org.slim3.datastore.Datastore.getOrNull(net.fivegoods.entity.impl.slim3.MissionExecutionModel.class, key);
	}
	@javax.ws.rs.Path("/{key}")
	@javax.ws.rs.DELETE
	public static void deleteByKeyString(
		@javax.ws.rs.PathParam("key") java.lang.String keyStr) {
		deleteByKey(com.google.appengine.api.datastore.KeyFactory.stringToKey(keyStr));
	}
	public static void deleteByKey(com.google.appengine.api.datastore.Key key) {
		org.slim3.datastore.Datastore.delete(key);
	}


	@javax.ws.rs.GET
	public static java.util.List<net.fivegoods.entity.impl.slim3.MissionExecutionModel> query(
		@javax.ws.rs.QueryParam("missionKey") java.lang.String missionKey,
		@javax.ws.rs.DefaultValue("EQUAL") @javax.ws.rs.QueryParam("missionKeyOp") java.lang.String missionKeyOp,
		@javax.ws.rs.QueryParam("volunteerKey") java.lang.String volunteerKey,
		@javax.ws.rs.DefaultValue("EQUAL") @javax.ws.rs.QueryParam("volunteerKeyOp") java.lang.String volunteerKeyOp,
		@javax.ws.rs.QueryParam("state") java.lang.Integer state,
		@javax.ws.rs.DefaultValue("EQUAL") @javax.ws.rs.QueryParam("stateOp") java.lang.String stateOp,
		@javax.ws.rs.QueryParam("sort") java.util.List<momo.slim3.SortParam> sortParams) {
		org.slim3.datastore.ModelQuery<MissionExecutionModel> _query_ = org.slim3.datastore.Datastore.query(MissionExecutionModel.class);
		if (missionKeyOp.equals("IS_NULL")) {
			_query_.filter("missionKey", com.google.appengine.api.datastore.Query.FilterOperator.EQUAL, null);
		} else if (missionKey != null) {
			_query_.filter("missionKey", com.google.appengine.api.datastore.Query.FilterOperator.valueOf(missionKeyOp), com.google.appengine.api.datastore.KeyFactory.stringToKey(missionKey));
		}
		if (volunteerKeyOp.equals("IS_NULL")) {
			_query_.filter("volunteerKey", com.google.appengine.api.datastore.Query.FilterOperator.EQUAL, null);
		} else if (volunteerKey != null) {
			_query_.filter("volunteerKey", com.google.appengine.api.datastore.Query.FilterOperator.valueOf(volunteerKeyOp), com.google.appengine.api.datastore.KeyFactory.stringToKey(volunteerKey));
		}
		if (stateOp.equals("IS_NULL")) {
			_query_.filter("state", com.google.appengine.api.datastore.Query.FilterOperator.EQUAL, null);
		} else if (state != null) {
			_query_.filter("state", com.google.appengine.api.datastore.Query.FilterOperator.valueOf(stateOp), state);
		}
		for (momo.slim3.SortParam _sort_ : sortParams) {
			_query_.sort(_sort_.getProperty(),
				_sort_.isAsc() ?
					com.google.appengine.api.datastore.Query.SortDirection.ASCENDING :
					com.google.appengine.api.datastore.Query.SortDirection.DESCENDING);
		}
		return _query_.asList();
	}

	public MissionExecutionModel() {
	}

	private static final String STATIC_KEY_NAME = "STATIC_KEY_NAME";
	public MissionExecutionModel(com.google.appengine.api.datastore.Key missionClientKey) {
		this.key = org.slim3.datastore.Datastore.createKey(missionClientKey, this.getClass(), STATIC_KEY_NAME);
	}

	@org.slim3.datastore.Attribute(primaryKey = true)
	private com.google.appengine.api.datastore.Key key;

	public com.google.appengine.api.datastore.Key getKey() {
		return key;
	}
	@javax.xml.bind.annotation.XmlElement
	public java.lang.String getKeyString() {
		return com.google.appengine.api.datastore.KeyFactory.keyToString(key);
	}
	public void setKey(com.google.appengine.api.datastore.Key key) {
		this.key = key;
	}




	private final org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.MissionModel> missionRef = new org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.MissionModel>(net.fivegoods.entity.impl.slim3.MissionModel.class);

	public org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.MissionModel> getMissionRef() {
		return this.missionRef;
	}

	private final org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.VolunteerModel> volunteerRef = new org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.VolunteerModel>(net.fivegoods.entity.impl.slim3.VolunteerModel.class);

	public org.slim3.datastore.ModelRef<net.fivegoods.entity.impl.slim3.VolunteerModel> getVolunteerRef() {
		return this.volunteerRef;
	}

	@org.slim3.datastore.Attribute	
@javax.ws.rs.FormParam("state")
	private int state;
	
	public void setState(int state) {
		this.state = state;
	}
	@javax.xml.bind.annotation.XmlElement
	public int getState() {
		return this.state;
	}


	@javax.ws.rs.POST
	public void save() {
		if (this.key != null) {
			throw new java.lang.IllegalStateException("key is not null.");
		}
		if (this.validator != null) {
			java.util.Set<javax.validation.ConstraintViolation<MissionExecutionModel>> violations = validator.validate(this);
			if (!violations.isEmpty()) {
				throw new javax.validation.ConstraintViolationException(new java.util.HashSet<javax.validation.ConstraintViolation<?>>(violations));
			}
		}
		org.slim3.datastore.Datastore.put(this);
	}
	@javax.ws.rs.Path("/{key}")
	@javax.ws.rs.PUT
	public void save(@javax.ws.rs.PathParam("key") java.lang.String keyStr) {
		this.key = com.google.appengine.api.datastore.KeyFactory.stringToKey(keyStr);
		if (this.validator != null) {
			java.util.Set<javax.validation.ConstraintViolation<MissionExecutionModel>> violations = validator.validate(this);
			if (!violations.isEmpty()) {
				throw new javax.validation.ConstraintViolationException(new java.util.HashSet<javax.validation.ConstraintViolation<?>>(violations));
			}
		}
		org.slim3.datastore.Datastore.put(this);
	}
	
	public void delete() {
		if (this.key != null) {
			deleteByKey(this.key);
		}
	}
}
